package com.kscrm.web.controller.web;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.kscrm.common.core.domain.R;
import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.web.domain.StatMap;
import com.kscrm.web.service.IStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kscrm.common.annotation.Log;
import com.kscrm.common.core.controller.BaseController;
import com.kscrm.common.enums.BusinessType;
import com.kscrm.web.domain.Stat;
import com.kscrm.common.utils.poi.ExcelUtil;

/**
 * 统计Controller
 * 
 * @author Dhq
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/web/stat")
public class StatController extends BaseController
{
    @Autowired
    private IStatService statService;

    /**
     * 查询统计列表
     */
    @GetMapping("/list")
    public R<List<StatMap>> list(Stat stat)
    {
        Long channelId = SecurityUtils.getChannelId();
        return R.ok(statService.selectStatListByTime(stat.getStartAt(), stat.getEndAt(), channelId));
    }

    /**
     * 导出统计列表
     */
    @Log(title = "统计导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stat stat)
    {
        Long channelId = SecurityUtils.getChannelId();
        List<StatMap> list = statService.selectStatListByTime(stat.getStartAt(), stat.getEndAt(), channelId);
        ExcelUtil<StatMap> util = new ExcelUtil<StatMap>(StatMap.class);
        util.exportExcel(response, list, "统计数据");
    }

}
